import request from "@/router/axios";
import avueDataAdapter from "@/utils/avueDataAdapter";
import activitiFormUtils from "./activitiFormUtils";

const optionSearchCache = {};

function loadSearchCache(columns) {
    let columnsMap = {};
    if (!columns) return columnsMap;
    for (let i = 0; i < columns.length; i++) {
        let column = columns[i];
        if (!column.prop) continue;
        if (!column.search) continue
        columnsMap[column.prop] = column.searchModel ? column.searchModel : "#{0}";
    }
    return columnsMap;
}

const defaultLoadParams={
    columnKey:"column",
}
const optionUtils = {

    /**
     * 获取搜索列信息
     * @param option
     * @param cacheKey
     * @returns {{}}
     */
    getSearchColumnMap(option, cacheKey) {
        let columnsMap = {};
        if (option == null) return columnsMap;
        let optionConfig = null;
        let key = cacheKey;
        if (option instanceof Function) {
            optionConfig = option();
        } else if (option instanceof Object) {
            optionConfig = option;
        } else if (option instanceof Array) {
            optionConfig = {column: option};
        } else {
            return columnsMap;
        }
        let columns = optionConfig.column;
        if (!columns) return columnsMap;

        if (!key)
            key = optionConfig.cacheKey;
        if (!key)
            return loadSearchCache(columns)
        if (optionSearchCache[key])
            return optionSearchCache[key];
        columnsMap = loadSearchCache(columns);
        optionSearchCache[key] = columnsMap;
        return optionSearchCache[key];
    },
    tableOnLoad(onLoadParams, callback) {
        if (onLoadParams.value) {
            loadValue(onLoadParams.value, onLoadParams.valueDefinition, callback)
            return
        }
        loadPage(onLoadParams, callback)
    }
}

/**
 * 加载分页信息
 * @param loadParams
 * @param callback
 */
function loadPage(loadParams,  callback) {
    loadParams=Object.assign(defaultLoadParams,loadParams)
    let pageParams = {}
    let formSearchParams = loadParams.data;
    const page = loadParams.page
    pageParams.current = page.currentPage
    pageParams.size = page.pageSize;
    let searchParams = {};
    const tableOption=loadParams.option instanceof Function?loadParams.option():loadParams.option
    let columnsMap = optionUtils.getSearchColumnMap( loadParams.option);
    for (const columnKey in formSearchParams) {
        if (!columnsMap[columnKey]) continue;
        searchParams["search_" + columnKey] = columnsMap[columnKey] + formSearchParams[columnKey]
    }
    const requestData = Object.assign(pageParams, formSearchParams, searchParams)
    if (loadParams.pageDefinition instanceof Function) {
        loadParams.pageDefinition(requestData).then(callBackRequest)
        return
    }
    request({
        url: loadParams.pageDefinition,
        method: 'get',
        data: data
    }).then(callBackRequest,callback)
    function callBackRequest(res) {
        let resultData = res.data.data;
        const records=resultData.records;
        avueDataAdapter.transformDataList(tableOption.column,records)
        callback({
            data: records,
            total: resultData.total
        })
    }


}


/**
 * 加载固定值信息
 * @param value
 * @param valueDefinition
 * @param callback
 */
function loadValue(value, valueDefinition, callback) {
    if (valueDefinition instanceof Function) {
        valueDefinition(value).then(res => callback(res.data.data))
        return
    }
    let data = {};
    data.value = value
    request({
        url: valueDefinition,
        method: 'get',
        data: data
    }).then(res => callback(res.data.data))

}



optionUtils.handlerFormConfigVariable=(formOption)=>{
    return  activitiFormUtils.handlerFormConfigVariable(formOption)
}

export default optionUtils
